package com.macro.mall.portal.controller;

import com.alibaba.fastjson.JSON;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;

import com.macro.mall.common.exception.ApiException;
import com.macro.mall.mapper.OmsCartItemMapper;
import com.macro.mall.model.OmsCartItem;
import com.macro.mall.portal.domain.OrderParam;
import com.macro.mall.portal.dto.PmsProductDto;
import com.macro.mall.portal.service.activityImpl.ActivityImpl;
import com.quyum.mall.market.entity.MarketSmsRound;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/secKill")
public class SecKillController {
    @Autowired
    private ActivityImpl activity;
    @Autowired
    private OmsCartItemMapper omsCartItemMapper;


    @ApiOperation("秒杀专场详情展示")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmsProductDto>> recommendList(@RequestParam(required = false,value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                 @RequestParam(required = false,value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                 @RequestParam Long roundId) {
        return this.activity.getDetails(pageSize, pageNum, roundId);
    }



    @ApiOperation("获取秒杀专场列表页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<MarketSmsRound>> list() {
        return this.activity.getListCommonResult(MarketSmsRound.TYPE.SEC_KILL.VALUE);
    }



    /**
     * 秒杀
     * @author zhouboyu
     * @Date 2020年10月22日
     * @param orderParam
     * @return
     */
    @ApiOperation("商品详情页生成订单")
    @RequestMapping(value = "/doSecKill", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CommonResult generateOrderItemDetails(@RequestBody OrderParam orderParam) {
        Map<String, Object> params = JSON.parseObject(JSON.toJSONString(orderParam),Map.class);
        Long id = null;
        try {
            id = this.activity.seckill(params);
        } catch (ApiException e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success(id);
    }

    /**
     * 购物车秒杀
     * @author zhouboyu
     * @Date 2020年10月22日
     * @param orderParam
     * @return
     */
    @ApiOperation("购物车秒杀")
    @RequestMapping(value = "/doCatItemSecKill", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public CommonResult doCatItemSecKill(@RequestBody OrderParam orderParam) {
        List<Long> cartIds = orderParam.getCartIds();
        OmsCartItem omsCartItem = this.omsCartItemMapper.selectByPrimaryKey(cartIds.get(0));
        orderParam.setQuantity(Long.valueOf(omsCartItem.getQuantity()));
        orderParam.setSkuId(omsCartItem.getProductSkuId());
        orderParam.setProductId(omsCartItem.getProductId());
        Map<String, Object> params = JSON.parseObject(JSON.toJSONString(orderParam),Map.class);
        Long id = null;
        try {
            id=this.activity.seckill(params);
        } catch (ApiException e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
        return CommonResult.success(id);
    }
}
